{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.advanced_activations import LeakyReLU, PReLU, ELU, ThresholdedReLU\n",
    "from keras import backend as K\n",
    "import json\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### LeakyReLU"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[advanced_activations.LeakyReLU.0] alpha=0.4**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, -0.5, -0.1, 1, 2]\n",
      "in shape: (6,)\n",
      "out shape: (6,)\n",
      "out: [0.0, 0.2, -0.2, -0.04, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "layer_0 = Input(shape=(6,))\n",
    "layer_1 = LeakyReLU(alpha=0.4)(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, -0.5, -0.1, 1, 2]\n",
    "data_in_shape = (6,)\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['advanced_activations.LeakyReLU.0'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PReLU"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[advanced_activations.PReLU.0] weights: alpha**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, -0.5, -0.1, 1, 2]\n",
      "in shape: (6,)\n",
      "out shape: (6,)\n",
      "out: [0.0, 0.2, -0.01, 0.003, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "layer_0 = Input(shape=(6,))\n",
    "layer_1 = PReLU()(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "alpha = np.array([-0.03, -0.02, 0.02, -0.03, -0.03, -0.01])\n",
    "model.set_weights([alpha])\n",
    "\n",
    "data_in = [0, 0.2, -0.5, -0.1, 1, 2]\n",
    "data_in_shape = (6,)\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['advanced_activations.PReLU.0'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in [alpha]],\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ELU"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[advanced_activations.ELU.0] alpha=1.1**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, -0.5, -0.1, 1, 2]\n",
      "in shape: (6,)\n",
      "out shape: (6,)\n",
      "out: [0.0, 0.2, -0.432816, -0.104679, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "layer_0 = Input(shape=(6,))\n",
    "layer_1 = ELU(alpha=1.1)(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, -0.5, -0.1, 1, 2]\n",
    "data_in_shape = (6,)\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['advanced_activations.ELU.0'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ThresholdedReLU"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[advanced_activations.ThresholdedReLU.0] theta=0.9**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in: [0, 0.2, 0.5, -0.1, 1, 2]\n",
      "in shape: (6,)\n",
      "out shape: (6,)\n",
      "out: [0.0, 0.0, 0.0, 0.0, 1.0, 2.0]\n"
     ]
    }
   ],
   "source": [
    "layer_0 = Input(shape=(6,))\n",
    "layer_1 = ThresholdedReLU(theta=0.9)(layer_0)\n",
    "model = Model(inputs=layer_0, outputs=layer_1)\n",
    "\n",
    "data_in = [0, 0.2, 0.5, -0.1, 1, 2]\n",
    "data_in_shape = (6,)\n",
    "print('in:', data_in)\n",
    "print('in shape:', data_in_shape)\n",
    "arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)\n",
    "result = model.predict(np.array([arr_in]))\n",
    "arr_out = result[0]\n",
    "data_out_shape = arr_out.shape\n",
    "print('out shape:', data_out_shape)\n",
    "data_out = format_decimal(arr_out.ravel().tolist())\n",
    "print('out:', data_out)\n",
    "\n",
    "DATA['advanced_activations.ThresholdedReLU.0'] = {\n",
    "    'input': {'data': data_in, 'shape': data_in_shape},\n",
    "    'expected': {'data': data_out, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/layers/advanced_activations.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"advanced_activations.LeakyReLU.0\": {\"input\": {\"data\": [0, 0.2, -0.5, -0.1, 1, 2], \"shape\": [6]}, \"expected\": {\"data\": [0.0, 0.2, -0.2, -0.04, 1.0, 2.0], \"shape\": [6]}}, \"advanced_activations.PReLU.0\": {\"input\": {\"data\": [0, 0.2, -0.5, -0.1, 1, 2], \"shape\": [6]}, \"weights\": [{\"data\": [-0.03, -0.02, 0.02, -0.03, -0.03, -0.01], \"shape\": [6]}], \"expected\": {\"data\": [0.0, 0.2, -0.01, 0.003, 1.0, 2.0], \"shape\": [6]}}, \"advanced_activations.ELU.0\": {\"input\": {\"data\": [0, 0.2, -0.5, -0.1, 1, 2], \"shape\": [6]}, \"expected\": {\"data\": [0.0, 0.2, -0.432816, -0.104679, 1.0, 2.0], \"shape\": [6]}}, \"advanced_activations.ThresholdedReLU.0\": {\"input\": {\"data\": [0, 0.2, 0.5, -0.1, 1, 2], \"shape\": [6]}, \"expected\": {\"data\": [0.0, 0.0, 0.0, 0.0, 1.0, 2.0], \"shape\": [6]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
